<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
          <template slot="menuRight">
            <el-button
              type="primary"
              size="small"
              @click.stop="refresh()"
              >重新生成营收日报</el-button
            >
          </template>
      </avue-crud>
    </basic-container>
    
    <open-dialog
      :DialogVisible.sync="refreshVisableDialog"
      title="选择线路"
      width="50%"
    >
      <el-form>
        <avue-form
          ref="form"
          v-model="refreshForm"
          :option="refreshOption"
        ></avue-form>
        <div class="bottom-menu-close">
          <el-button type="primary" size="small" :loading="btnloading" @click="refreshSubmit">确认</el-button>
          <el-button type="primary" size="small" @click="refreshVisableDialog = false"
            >取消</el-button
          >
        </div>
      </el-form>
    </open-dialog>
  </div>
</template>

<script>
  import baseCrud from '@/mixins/base-crud'
  import {refreshStationincome} from '@/api/subwayRat/sw-st-report/stRevenueDailyreport'
  import { refreshOption } from "@/const/crud/subwayRat/sw-st-report/stRevenueDailyreport";
  import { dateFormat } from "@/util/date";
  import moment from 'moment';

  export default {
    name: 'stRevenueDailyreport',
    mixins: [baseCrud('subwayRat/sw-st-report/stRevenueDailyreport')],
    data() {
      return {
        refreshForm: {},
        refreshOption: refreshOption,
        refreshVisableDialog: false,
        btnloading: false
      }
    },
    computed: {
      permissionList() {
        return {
        }
      }
    },
    methods: {
      refresh(){
        for (var i in this.refreshForm) {
            this.refreshForm[i] = null;
        }
        this.refreshVisableDialog = true;
      },
      refreshSubmit(){
        let now = new Date();
        let newDate = moment(this.refreshForm.startDateStr).add(10,'days').format("YYYY-MM-DD");
        this.$refs.form.validate(vaild=>{
          if(vaild){
            if(
              this.refreshForm.endDateStr > dateFormat(now).substr(0, 10)
            ){
              this.$message.warning("结束日期不能大于当前日期！");
            }else if(newDate<this.refreshForm.endDateStr){
              this.$message.warning("选择开始日期和结束日期的时间范围不能超过10天！");
            }else{
              this.$confirm("确认重新生成报表?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }).then(() => {
                this.btnloading = true;
                let param = {
                  lineCode: this.refreshForm.lineCode,
                  startDateStr: this.refreshForm.startDateStr,
                  endDateStr: this.refreshForm.endDateStr
                };
                refreshStationincome(param).then((response)=>{
                  this.$message.success("重新生成营收日报表成功");
                  this.btnloading = false;
                  this.refreshVisableDialog = false;
                })
                .catch(error=> {
                  if(error.message.indexOf('timeout')!=-1){
                    this.$message.warning('重新生成生成需要一定时间，请稍后查看!');
                    this.btnloading = false;
                    this.refreshVisableDialog = false;
                  }else{
                    this.$message.error('重新生成营收日报表失败!')
                    this.btnloading = false;
                    this.refreshVisableDialog = false;
                  }
                });
              });
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.bottom-menu-close {
  text-align: center;
}
</style>
